package com.tomecode.tools.maven.settings.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JOptionPane;

import com.tomecode.tools.maven.settings.SettingsFactory;
import com.tomecode.tools.maven.settings.gui.tabs.TabManager;
import com.tomecode.tools.maven.settings.gui.tray.Tray;
import com.tomecode.tools.maven.settings.model.Settings;
import com.tomecode.util.gui.Frame;
import com.tomecode.util.gui.PanelFactory;
import com.tomecode.util.gui.component.Button;

/**
 * Editor (main form)
 * 
 * 
 * @author Frastia Tomas
 * 
 */
public final class Desktop extends Frame {

	private static final long serialVersionUID = 7845587976318619445L;

	private static Desktop me;

	private final TabManager tabManager;

	private final Button bSave;

	private final Button bCancel;

	private Desktop(final Settings settings) {
		super("Maven Settings Editor", 550, 700, true, false);
		addWindowListener(new WindowAdapter() {
			public final void windowClosing(WindowEvent winEvt) {
				Tray.getInstance().unblock();
			}
		});

		tabManager = new TabManager(settings);
		addToContainer(tabManager, BorderLayout.CENTER);

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		bSave = new Button("Save", new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();

				try {
					SettingsFactory.saveSettingFile(settings.getFile(), settings.getRepository());
				} catch (IOException ex) {
					JOptionPane.showOptionDialog(Desktop.getComponent(), "Failed save changes: " + ex.getMessage(), "Failed save changes", JOptionPane.ERROR_MESSAGE, JOptionPane.ERROR_MESSAGE, null, null, null);
				}

				Tray.getInstance().reload();
			}
		});
		bCancel = new Button("Cancel", new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();
				Tray.getInstance().unblock();
			}
		});
		addToContainer(PanelFactory.createButtonsPanel(2, 2, 2, 2, bSave, bCancel), BorderLayout.SOUTH);
	}

	/**
	 * show frame
	 */
	public static final void showMe(final Settings settings) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				me = new Desktop(settings);
				me.setVisible(true);
			}
		});
	}

	public static final Frame getComponent() {
		return me;
	}

	public static final void main(String[] args) {
		//
		// try {
		// showMe(SettingsFactory.loadSettings());
		// } catch (DocumentException e) {
		// showErr(e.getMessage());
		// } catch (FileNotFoundException e) {
		// showErr(e.getMessage());
		// } catch (IOException e) {
		// showErr(e.getMessage());
		// }
		Tray.getInstance();
	}

	// private static final void showErr(String errMsg) {
	// FrmSettingsXmlPath.showMe(errMsg, new HideNotifiListener() {
	//
	// public final void hideForm(Object settings) {
	// if (settings == null) {
	// System.exit(0);
	// } else {
	// showMe((Settings) settings);
	// }
	// }
	// });
	// }
}
